﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.ProductionSystem
{
    /// <summary>
    /// 工单接口
    /// </summary>
    public class ProductionServices : ApplicationService, IProductionServices
    {
        private readonly IRepository<Production, int> _productionRepository;

        public ProductionServices(IRepository<Production, int> productionRepository)
        {
            _productionRepository = productionRepository;
        }

        public async Task<int> AddProdction(ProdctionDTO prodction)
        {
            var entity = ObjectMapper.Map<ProdctionDTO, Production>(prodction);
            var result = await _productionRepository.InsertAsync(entity);
            return result == null ? 0 : 1;
        }

        public async Task<int> DeleteProdction(int id)
        {
            var result = _productionRepository.DeleteAsync(m => m.Id == id);
            return result == null ? 0 : 1;
        }

        public async Task<ProdctionDTO> QueryProdction(int id)
        {
            var productionList = await _productionRepository.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<Production, ProdctionDTO>(productionList);
            return result;
        }

        public async Task<List<ProdctionDTO>> ShowProdction(int pageIndex = 1, int pageSize = 10, string? orderName = "")
        {
            var productionList = await _productionRepository.GetListAsync();
            productionList = productionList.WhereIf(!string.IsNullOrEmpty(orderName), m => m.OrderName.Contains(orderName)).ToList();
            var result = ObjectMapper.Map<List<Production>, List<ProdctionDTO>>(productionList);
            return result;
        }

        public async Task<int> UpdateProdction(ProdctionDTO prodction)
        {
            var entity = ObjectMapper.Map<ProdctionDTO, Production>(prodction);
            var result = await _productionRepository.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
